﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExpressionLanguage
{

    public interface IMovieDatabase
    {
        void ListMovie();
    }

    public class SimpleMovieList : IMovieDatabase
    {
        public SimpleMovieList( List<string> movieList )
        {
            MovieList_ = movieList;
        }

        public void ListMovie()
        {
            Console.WriteLine("Gone with wind!");
            Console.WriteLine("The braver!");
            Console.WriteLine("Lord of the ring!");
            foreach(var e in MovieList_)
                Console.WriteLine(e);
        }

        private List<string> MovieList_;
    }

    public class MovieWebsite
    {
        
        public IMovieDatabase MovieDatabase
        {
            get;
            set;
        }

        public string WebsiteName
        {
            get;
            set;
        }

        public void Show()
        {
            MovieDatabase.ListMovie();
        }
    }

    class Program
    {
        public static bool MakeList(List<ExpressionInterpreter.ExecutionStack.Variable> parameters, out ExpressionInterpreter.ExecutionStack.Variable result)
        {
            List<string> ret = new List<string>();
            foreach(var e in parameters)
            {
                string str;
                if(e.AsString(out str))
                {
                    ret.Add(str);
                }
            }
            result = new ExpressionInterpreter.ExecutionStack.Variable(ret);
            return true;
        }

        static void Main(string[] args)
        {
            ExpressionInterpreter.ExecutionStack stack = new ExpressionInterpreter.ExecutionStack();
            ExpressionInterpreter.GlobalFunction func = new ExpressionInterpreter.GlobalFunction();
            CoreLibrary.LoadLibrary(func,stack);
            MathLibrary.LoadLibrary(func, stack);
            StringLibrary.LoadLibrary(func, stack);
            func.RegisterGlobalFunction("tostrlist", MakeList);
            ErrorCollector error = ExpressionInterpreter.ExecuteFromFile("script.ioc",func,stack);
            MovieWebsite website = (MovieWebsite)stack.LookUpAsObject("root");
            website.Show();
            return;
        }
    }
}
